// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// w [author] rhyme_lph
// m [email]  rhymelph@gmail.com
// m [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/bank_detail_record_item_entity.dart';
import 'package:finance_app/src/entities/bank_reconciliation_record_entity.dart';
import 'package:finance_app/src/provider/all_reports/bank_reconciliation/reconcile_account_confirm_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/utils/cash_utils.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/list_item/value_border_Item.dart';
import 'package:flutter/material.dart';

@RouterMeta(paramName: "reconcileAccountConfirmPage")
class ReconcileAccountConfirmPage extends StatefulWidget {
  final String title;
  final int reconcileId;
  final String closingDate;
  final String beginningBalance;
  final String statementBalance;
  final List<BankReconciliationRecordEntity> records;
  final List<BankDetailRecordItemEntity> selectReconcileList;
  final List<int> paymentIds;

  const ReconcileAccountConfirmPage(
      {Key key,
      this.title,
      this.reconcileId,
      this.paymentIds,
      this.records,
      this.selectReconcileList,
      this.closingDate,
      this.beginningBalance,
      this.statementBalance})
      : super(key: key);

  @override
  _ReconcileAccountConfirmPageState createState() =>
      _ReconcileAccountConfirmPageState();
}

class _ReconcileAccountConfirmPageState
    extends State<ReconcileAccountConfirmPage>
    with
        PagePresenter<ReconcileAccountConfirmPage,
            ReconcileAccountConfirmProvider> {
  @override
  void initState() {
    super.initState();
    provider.reconcileId = widget.reconcileId;
    provider.records = widget.records;
    provider.selectReconcileList = widget.selectReconcileList;
    provider.paymentIds = widget.paymentIds;
  }

  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: '${widget.title}', actions: [
        NormalConfirmTextButton(
          onTap: provider.onConfirm,
        ),
      ]);

  @override
  Widget get buildWidget => ListView(
        children: [
          AppSpaces.h20,
          ValueBorderItem(
            title: S.current.closing_date,
            value: widget.closingDate ?? '',
          ),
          AppSpaces.h30,
          _buildBetweenText(
            title: S.current.beginning_balance,
            value: widget?.beginningBalance ?? 'balance',
          ),
          listBuilder<num>(
            selector: (_, __) => __.reconciledDeposits,
            builder: (BuildContext context, num value, Widget child) =>
                _buildBetweenText(
                    title: S.current.reconciled_deposits, amount: value),
          ),
          listBuilder<num>(
            selector: (_, __) => __.reconciledWithdrawals,
            builder: (BuildContext context, num value, Widget child) =>
                _buildBetweenText(
                    title: S.current.reconciled_withdrawals,
                    amount: value,
                    isNav: true),
          ),
          _buildBetweenText(
              title: S.current.statement_balance,
              value:
                  '${CashUtils.formatCashAmountString(widget.statementBalance)}',
              isBlack: true),
        ],
      );

  Widget _buildBetweenText(
          {String title,
          String value,
          num amount,
          bool isBlack = false,
          String operation,
          bool isNav}) =>
      Container(
        padding: EdgeInsets.symmetric(
          horizontal: getDp(35),
          vertical: getDp(12),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              title,
              style: TextStyle(
                color: isBlack ? aceBlackColor : aceDarkGrayColor,
                fontSize: getSp(16),
                fontWeight: FontWeight.w700,
                height: 14 / 16,
              ),
            ),
            Text.rich(TextSpan(
                text: value,
                children: [
                  if (amount != null)
                    TextSpan(
                      text: operation ??
                          CashUtils.operation(isNav == true ? -1 : amount),
                      style: TextStyle(
                        color: CashUtils.getColor(isNav == true ? -1 : amount),
                      ),
                    ),
                  if (amount != null)
                    TextSpan(
                        text: '${CashUtils.ignoreFormatCashAmount(amount)}',
                        style: TextStyle()),
                ],
                style: TextStyle(
                    color: isBlack ? aceBlackColor : aceDarkGrayColor,
                    fontSize: getSp(18),
                    fontWeight: FontWeight.w700,
                    height: 14 / 18,
                    fontFamily: "DinRoundPro")))
          ],
        ),
      );
}
